package com.buddies.lottery.ui.lottery.bet;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.buddies.lottery.R;
import com.buddies.lottery.base.BaseActivity;
import com.buddies.lottery.base.PagerAdapter;
import com.buddies.lottery.data.bean.Lottery;
import com.buddies.lottery.ui.lottery.select.LotterySelectActivity;
import com.buddies.lottery.widget.OnTabClickListener;

import java.util.ArrayList;

public class LotteryBetRecordActivity extends BaseActivity implements TabLayout.BaseOnTabSelectedListener, OnTabClickListener {

    private static final int REQUEST_SELECT_LOTTERY = 1001;

    private ArrayList<String> mTabTexts = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    private ViewPager mViewPager;
    private PagerAdapter mPagerAdapter;
    private TabLayout mTabLayout;

    private Lottery mLottery;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bet_record;
    }

    @Override
    protected void initView() {
        setUp();
        setTitle("投注记录");
        setOnTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onTabClick();
            }
        });

        TextView btnFilter = findViewById(R.id.tv_action);
        btnFilter.setText("彩种筛选");
        btnFilter.setVisibility(View.VISIBLE);
        btnFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LotterySelectActivity.launch(getActivity(), REQUEST_SELECT_LOTTERY);
            }
        });

        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            mLottery = (Lottery) intent.getSerializableExtra("lottery");
        }

        mTabTexts.add("未开奖");
        mTabTexts.add("已中奖");
        mTabTexts.add("未中奖");
        mTabTexts.add("已撤单");

        mFragments.add(LotteryBetRecordFragment.newInstance(0));
        mFragments.add(LotteryBetRecordFragment.newInstance(1));
        mFragments.add(LotteryBetRecordFragment.newInstance(2));
        mFragments.add(LotteryBetRecordFragment.newInstance(3));

        mPagerAdapter = new PagerAdapter(getActivity(), getSupportFragmentManager(), mTabTexts, mFragments);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOffscreenPageLimit(mTabTexts.size());
        mTabLayout.setupWithViewPager(mViewPager);

        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            mTabLayout.getTabAt(i).setCustomView(mPagerAdapter.getTabView(i));
        }

        mTabLayout.addOnTabSelectedListener(this);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        onTabClick();
    }

    private Fragment getCurrentTabFragment() {
        if (mPagerAdapter != null) {
            return (Fragment) mPagerAdapter.instantiateItem(mViewPager, mTabLayout.getSelectedTabPosition());
        }
        return null;
    }

    @Override
    public void onTabClick() {
        Fragment fragment = getCurrentTabFragment();
        if (fragment instanceof OnTabClickListener) {
            ((OnTabClickListener) fragment).onTabClick();
        }
    }

    private void forceRefresh() {
        Fragment fragment = getCurrentTabFragment();
        if (fragment instanceof LotteryBetRecordFragment) {
            ((LotteryBetRecordFragment) fragment).forceRefresh();
        }
    }

    public Lottery getLottery() {
        return mLottery;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SELECT_LOTTERY && resultCode == RESULT_OK && data != null) {
            mLottery = (Lottery) data.getSerializableExtra("lottery");
        } else {
            mLottery = null;
        }

        if (mFragments != null) {
            for (Fragment fragment : mFragments) {
                if (fragment instanceof LotteryBetRecordFragment) {
                    ((LotteryBetRecordFragment) fragment).clearData();
                }
            }
        }

        forceRefresh();
    }

    public static void launch(Context context) {
        launch(context, null);
    }

    public static void launch(Context context, Lottery lottery) {
        launch(context, lottery, null);
    }

    public static void launch(Context context, Lottery lottery, String period) {
        Intent intent = new Intent();
        intent.putExtra("lottery", lottery);
        intent.putExtra("period", period);
        intent.setClass(context, LotteryBetRecordActivity.class);
        context.startActivity(intent);
    }

    public int getCurrentTab() {
        return mViewPager.getCurrentItem();
    }
}
